/* 
    请求模块配置文件
*/
import axios from "axios";
import store from '@/store/index'
import { tansParams } from "./util";

const request = axios.create({
    baseURL: 'http://192.168.8.237:8002/',
    timeout: 2000,
})

// 请求拦截器
request.interceptors.request.use(function (config) {
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded'

    const { user } = store.state
    if (user && user.token) {
        config.headers['token'] = user.token
        // config.headers.Authorization = `Bearer ${user.token}`
    }

    // 将传的数据拼接到接口后面
    if (config.param) {
        let url = config.url + '?' + tansParams(config.param);
        url = url.slice(0, -1);
        config.param = {};
        config.url = config.baseURL + url;
    }
    if (config.data) {
        let url = config.url + '?' + tansParams(config.data);
        url = url.slice(0, -1);
        config.data = {};
        config.url = config.baseURL + url;
    }

    return config
}, function (error) {
    return Promise.reject(error)
})


//响应拦截器


export default request